package cn.baiweigang.uu.service.strategy;

import cn.baiweigang.uu.db.dataobj.StockInfoDo;
import cn.baiweigang.uu.db.repository.HistoryDayInfoRepository;
import cn.baiweigang.uu.enums.ExtKeyEnum;
import cn.baiweigang.uu.utils.DateUtil;
import cn.baiweigang.uu.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
public abstract class StrategyBaseService {

    @Autowired
    private HistoryDayInfoRepository historyDayInfoRepository;

    protected StockInfoDo queryPreDay(StockInfoDo stockInfoDo, int days) {
        String ds = DateUtil.getAddDays(stockInfoDo.getDs(), days);
        int limit = 0 - days;
        List<StockInfoDo> stockInfoDoList = historyDayInfoRepository.queryByDmBeforeDS(stockInfoDo.getDm(), ds, limit);
        return stockInfoDoList.get(limit - 1);
    }


    protected boolean isST(StockInfoDo stockInfoDo) {
        if (stockInfoDo == null) {
            return false;
        }
        if (StringUtil.isBlank(stockInfoDo.getMc())) {
            return false;
        }
        return stockInfoDo.getMc().indexOf("ST") > -1 ||
                stockInfoDo.getMc().indexOf("st") > -1;
    }

    //把Str按数字比较 -1 0 1代表 d1小于、等于、大于d2
    public int compareTo(StockInfoDo stockInfoDo, ExtKeyEnum extKey1, ExtKeyEnum extKey2) {
        if (stockInfoDo == null) {
            return 1;
        }
        String value1 = stockInfoDo.getExtByKey(extKey1.getCode());
        String value2 = stockInfoDo.getExtByKey(extKey2.getCode());
        return compareTo(value1, value2);
    }

    public int compareTo(StockInfoDo stockInfoDo, String value1, ExtKeyEnum extKey2) {
        if (StringUtil.isBlank(stockInfoDo.getExtByKey(extKey2.getCode())) ||
                StringUtil.isBlank(value1)) {
            log.error("有扩展值 不存在  请检查 dm=" + stockInfoDo.getDm());
            return 1;
        }
        String value2 = stockInfoDo.getExtByKey(extKey2.getCode());
        stockInfoDo.setStrategy(value2);
        return compareTo(value1, value2);
    }


    public int compareTo(String value1, String value2) {
        if (StringUtil.isBlank(value1) || StringUtil.isBlank(value2)) {
            return 1;
        }
        Double d1 = Double.parseDouble(value1);
        Double d2 = Double.parseDouble(value2);
        if (d1 > d2) {
            return 1;
        }
        return -1;
    }
}
